#!/bin/bash
# Version: 3.2.9
# Publisher: argor
# Phone: 15389222587

PUBLISHDIR=/.test

################## checks ###############################
if [ ! -d $PUBLISHDIR ]; then
    echo "请设定发布目录！"
    exit 1
fi
#check for root user
if [ "$(id -u)" -ne 0 ] ; then
    echo "You must run this script as root. Sorry!"
    exit 2
fi
################## checks ###############################

################## variables defines ###############################
#declare -a jars
#jars=`tree -if ${PUBLISHDIR}/* | grep ".jar$"`
#echo $jars
declare -a shs
shs=`tree -if ${PUBLISHDIR}/* | grep ".sh$"`
#echo $shs
################## variables defines ###############################


################## son function defines ###############################
# FUNCTION:	getShellScript
# RETURN:	dirname + basename
# When you operate application one by one, this function will find which command will be running
# after you want start, stop, restart or status.
function getShellScript() {
    tree -if ${PUBLISHDIR}/* | grep "\.sh$" | grep $1
}
################## son function defines ###############################


################## main function defines ###############################
function start() {
    distSH=`getShellScript $1`   
    #echo $distSH
    $distSH start
}

function stop() {
    distSH=`getShellScript $1`   
    #echo $distSH
    $distSH stop
}

function status() {
    distSH=`getShellScript $1`   
    #echo $distSH
    $distSH status
}

function restart() {
    distSH=`getShellScript $1`   
    #echo $distSH
    $distSH restart
}

# Start all application in the distation directorys where args is null.
function startAll() {
    for e in ${shs}; do
        #echo "startAll, command: " $e
        bash $e start
        sleep 1
    done
}

function restartAll() {
    for e in ${shs}; do
        #echo "startAll, command: " $e
        bash $e restart
        sleep 1
    done
}

function stopAll() {
    for e in ${shs}; do
        #echo "startAll, command: " $e
        bash $e stop
        sleep 1
    done
}

function statusAll() {
    for e in ${shs}; do
        #echo "startAll, command: " $e
        bash $e status
        sleep 1
    done
}

function help() {
    if [ -r ~/.mainrc/README ]; then
        echo -e "    Please read file named README.txt on current directory."
	cat ~/.mainrc/README
    else
        echo -e "    Please find file named README.TXT."
    fi
}
################## main function defines ###############################


if [ -z $1 ]; then
    #echo "Warnings: args is null, shell will start all application now."
    startAll
else if ! [[ $1 =~ ^-.* ]]; then
    echo -e "要么不带选项，要么是以 - 开头。\n 键入 $0 -h 获取帮助"
else
    #echo "args is not null: $@"
    while getopts ":s:t:p:r:RSTPhva" opt; do
        #echo "> opt = $opt"
        case "$opt" in
        # Valid options.
        "t") # start
            #echo -e "\targs: $OPTARG"
            #echo -e "\t\$OPTIND is $OPTIND"
	    start $OPTARG
            ;;
        "p") # stop
            #echo -e "\targs: $OPTARG"
            #echo -e "\t\$OPTIND is $OPTIND"
	    stop $OPTARG
            ;;
        "r") # restart
            #echo -e "\targs: $OPTARG"
            #echo -e "\t\$OPTIND is $OPTIND"
	    restart $OPTARG
            ;;
        "s") # status
            #echo -e "\targs: $OPTARG"
            #echo -e "\t\$OPTIND is $OPTIND"
	    status $OPTARG
            ;;
        "R") # restart all jar.
            #echo -e "\targs: $OPTARG"
            #echo -e "\t\$OPTIND is $OPTIND"
	    restartAll
            ;;
        "S") # stop all jar.
            #echo -e "\targs: $OPTARG"
            #echo -e "\t\$OPTIND is $OPTIND"
	    statusAll
            ;;
        "T") # start all jar.
            #echo -e "\targs: $OPTARG"
            #echo -e "\t\$OPTIND is $OPTIND"
	    startAll
            ;;
        "P") # stop all jar.
            #echo -e "\targs: $OPTARG"
            #echo -e "\t\$OPTIND is $OPTIND"
	    stopAll
            ;;
        "v") # Not silent, without arg. And invalid option.
            sed -n -e '2p' $0 | cut -d '#' -f2
            ;;
        "a") # Not silent, without arg. And invalid option.
            sed -n -e '3p' $0 | cut -d '#' -f2
            ;;
        "h") # Not silent, without arg. And invalid option.
            help
            ;;
        "?")    # Not silent, without arg. And invalid option.
            echo -e "Invalid option $OPTARG"
            echo -e "    Please read file named README."
            #echo -e "\t\$OPTIND is $OPTIND"
            ;;
        "*")    # Impossible to implement.
            echo -e "Unkown error from arg."
            #echo -e "\t\$OPTIND is $OPTIND"
            ;;
        esac
    done
fi;fi


